package com.swpu.project.controller;

import com.swpu.project.common.BaseResponse;
import com.swpu.project.mapper.MenuMapper;
import com.swpu.project.mapper.RoleMapper;
import com.swpu.project.model.entity.Menu;
import com.swpu.project.model.entity.User;
import com.swpu.project.model.page.PageQuery;
import com.swpu.project.service.IMenuService;
import com.swpu.project.service.IRoleService;
import com.swpu.project.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys")
public class SysController {
    @Resource
    IUserService iUserService;
    @Resource
    IRoleService iRoleService;
    @Resource
    IMenuService iMenuService;
    @Resource
    RoleMapper roleMapper;
    @Resource
    MenuMapper menuMapper;

    @GetMapping("/user/query")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse queryUserInform(@RequestBody PageQuery pageQuery) {
        return iUserService.queryUserInform(pageQuery);

    }

    /* 前端传
     * */
    @PutMapping("/user/updaate")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse updateUser(@RequestBody User user) {
        return BaseResponse.iFSuccess(iUserService.updateById(user));
    }

    @PostMapping("/user/add")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse addUser(@RequestBody User user) {
        return iUserService.saveMyUser(user);
    }

    @GetMapping("/role/query")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse queryRole(@RequestBody PageQuery pageQuery) {

        return iRoleService.queryPage(pageQuery);


    }
/*
*   在查看系统用户时对用户角色进行管理
* */
    @PostMapping("/role/user/add")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse addRole(@RequestParam String userId, @RequestParam List<String> roleIds)
    {

        return BaseResponse.iFSuccess(roleMapper.addBatchRoleForUser(userId,roleIds));
    }

    @PutMapping("/role/user/update")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse updateRole(@RequestParam String userId, @RequestParam Map<String,String> roleIds)
    {

        return BaseResponse.iFSuccess(roleMapper.updateRoleForUser(userId,roleIds));
    }

    @DeleteMapping("/role/user/delete")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse deleteRole(String userId, List<String> roleIds)
    {

        return BaseResponse.iFSuccess(roleMapper.dropBatchRoleForUser(userId,roleIds));
    }

   /*
   * 查看角色对角色进行权限管理
   *
   * */

    @PostMapping("/menu/role/add")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse addMenu(@RequestParam String roleId, @RequestParam List<String> menuIds)
    {

        return BaseResponse.iFSuccess(menuMapper.addBatchForRole(roleId,menuIds));
    }

    @PutMapping("/menu/role/update")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse updateMenu(@RequestParam String roleId, @RequestParam Map<String,String> menuIds)
    {

        return BaseResponse.iFSuccess(menuMapper.updateMenuForRole(roleId,menuIds));
    }

    @DeleteMapping("/menu/role/delete")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse deleteMenuForRole(String roleId, List<String> menuIds)
    {

        return BaseResponse.iFSuccess(menuMapper.deleteBatchForRole(roleId,menuIds));
    }
     /*
     *  对权限进行管理
     * */
    @GetMapping("/menu/query")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
  BaseResponse queryMenu(@RequestBody PageQuery pageQuery)
    {
       pageQuery.initSqlLimit();
      return   BaseResponse.ok(
              menuMapper.queryPageMenu(pageQuery.getQueryBy(), pageQuery.getSortBy(),
                      pageQuery.getPageStart(), pageQuery.getPageEnd()));

    }

    @PostMapping("/menu/add")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse addMenu(@RequestBody List<Menu> menus)
    {
        return   BaseResponse.ok(
       iMenuService.saveBatch(menus));

    }
    @PutMapping("/menu/update")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse updateMenu(@RequestBody List<Menu> menus)
    {
        return   BaseResponse.ok(
       iMenuService.updateBatchById(menus));

    }
 @DeleteMapping("/menu/delete")
    @PreAuthorize("@verf.hasAuthorityFor('sys_manage')")
    BaseResponse deleteMenu(@RequestBody List<Integer> menuIds)
    {

       return BaseResponse.iFSuccess(iMenuService.lambdaUpdate().set(Menu::getDelFlag,1).in(Menu::getId,menuIds).update());

    }




}
